namespace SubhadraSolutions.Sharp.Logging.LogSubscribers
{
    using SubhadraSolutions.Sharp.Logging.Contracts;
    using System.Collections.Generic;
    using System.Linq;

    public static class LogItemFilterHelper
    {
        #region Methods

        public static IEnumerable<LogStoreItem> Filter(IEnumerable<LogStoreItem> input,
            ILogSubscriber Subscriber)
        {
            return input.Where(item => shouldFilter(item.Severity, item.Categories, Subscriber));
        }

        private static bool shouldFilter(LogItemSeverity type, LogItemCategories categories, ILogSubscriber Subscriber)
        {
            return
                (
                    (type == LogItemSeverity.INFO && Subscriber.IsInfoEnabled) ||
                    (type == LogItemSeverity.DEBUG && Subscriber.IsDebugEnabled) ||
                    (type == LogItemSeverity.WARN && Subscriber.IsWarnEnabled) ||
                    (type == LogItemSeverity.ERROR && Subscriber.IsErrorEnabled) ||
                    (type == LogItemSeverity.FATAL && Subscriber.IsFatalEnabled)
                    )
                    &&
                    (((int)categories&(int)Subscriber.EnabledCategories)>0);
        }

        #endregion Methods
    }
}